const path = require("path");
const httpProxy = require('http-proxy');
const proxy = httpProxy.createProxyServer({}); // 新建一个代理 Proxy Server 对象
const proxyContorller = (req, res) => {
    return (req, res, next) => {
        var host = req.headers.host, ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
        console.log("client ip:" + ip + ", host:" + host);
        if (1) {
            // res.sendFile(path.join(process.cwd(), "dist/views", "stopIphone.html"));
            switch (host) {
                // case 'localhost:8092': proxy.web(req, res, { target: 'http://localhost:8091' });
                // break;
                // case '127.0.0.1:8091': proxy.web(req, res, { target: 'http://xyd.bbbbb.com:82' });
                // break;
                // case 'vps.cccc.com': proxy.web(req, res, { target: 'http://xyd.bbbbb.com:8080' });
                // break;
                // case 'dddd.com': proxy.web(req, res, { target: 'http://localhost:8091' });
                // break;
                // case 'www.dddd.com': proxy.web(req, res, { target: 'http://localhost:81' });
                // break;
                // case 'eeeeee.com.cn': proxy.web(req, res, { target: 'http://localhost:8091' });
                // break;
                // case 'www.eeee.com.cn:8091': proxy.web(req, res, { target: 'http://eeeee.com.cn:8082' });
                // break;
                // default: proxy.web(req, res, { target: 'http://localhost:8091' });
                // break;
                // res.writeHead(200, {
                // 'Content-Type': 'text/plain'
                // });
                // res.end('Welcome to my server!');
                // next();
            }
            next();
        } else {
            next();
        }
    };
};



// 捕获异常
proxy.on('error', function (err, req, res) {
    res.writeHead(500, {
        'Content-Type': 'text/plain'
    });
    res.end('Something went wrong. And we are reporting a custom error message.');
});


module.exports = proxyContorller;